{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "%%html\n",
    "<!-- （勿改动，执行即可）执行更改背景 -->\n",
    "<link rel=\"stylesheet\" href=\"exam.css\" type=\"text/css\">\n",
    "<h1 style=\"color: red;\">注意单元格的第一行不能改动，否则会影响自动打分</h1>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 始001（勿改动，执行即可）\n",
    "e = %env\n",
    "_which_= \"A\"  # 卷号\n",
    "import PandasCourse as PC\n",
    "from IPython.display import Markdown\n",
    "Markdown(PC.msgs['opening'].format(w=_which_, d=e['HOMEDRIVE']+ e['HOMEPATH']))    "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 本试题参考数据信息：    \n",
    "> 1. df_C1:  作者地址(Author Address)         \n",
    "> 2. text:  所有作者地址 **文本数据**     \n",
    "> 3. info: 所有作者地址 **列表数据**\n",
    "> 4. AU: 作者数据信息 **列表数据**\n",
    "\n",
    "* **勿改动部分如稍有不慎改动，请重新下载该文件，浪费时间后果自负**\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 0-1 数据准备\n",
    "# （勿改动，执行即可） 请观察text文本后再进行答题\n",
    "import pandas as pd\n",
    "df = pd.read_csv(\"WOS_2021.csv\",index_col=[0])\n",
    "df_C1 = df[['PY','C1']]\n",
    "text = '; '.join(df_C1.fillna(\"0\")['C1'].tolist())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Q1（20分） 🌶 易\n",
    "* 查找<font style=\"color:red\">\"China\"</font>的次数\n",
    "\n",
    "--------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A-1 查找\"China\"的次数\n",
    "phrase = \"China\"\n",
    "freq_table_phrase = text.count(phrase)\n",
    "freq_table_phrase"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Q2 （20分） 🌶 易\n",
    "\n",
    "------------\n",
    "* 用英文 <font style=\"color:red\">\"; \\[ \"</font> 拆分,生成list_split列表，每一个句子将成为列表中独立的元素\n",
    "* 答案示例：\n",
    "```\n",
    "['[Han, Xueying; Stocking, Galen; Gebbie, Matthew A.; Appelbaum, Richard P.] Univ Calif Santa Barbara, Ctr Nanotechnol Soc, Santa Barbara, CA 93106 USA',\n",
    " 'Stocking, Galen] Univ Calif Santa Barbara, Dept Polit Sci, Santa Barbara, CA 93106 USA',\n",
    " 'Gebbie, Matthew A.] Univ Calif Santa Barbara, Dept Mat, Santa Barbara, CA 93106 USA',\n",
    " 'Appelbaum, Richard P.] Univ Calif Santa Barbara, Global & Int Studies, Santa Barbara, CA 93106 USA',\n",
    " 'De Castell, Suzanne] Univ Ontario, Inst Technol, Fac Educ, 11 Simcoe St N, Oshawa, ON L1H 7L7, Canada',\n",
    " 'Larios, Hector] Simon Fraser Univ, Sch Interact Arts Technol, Surrey, BC V3T 0A3, Canada',\n",
    " ...省略]\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# B-1 用英文 \"; [ \" 拆分,生成list_split列表，每一个句子将成为列表中独立的元素\n",
    "list_split =text.split(\"; [\")\n",
    "list_split"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Q3 20分 易\n",
    "\n",
    "-------------\n",
    "* 筛选作者所有地址列表info中出现China的内容，组成新的列表(20分)\n",
    "* 答案示例：\n",
    "```\n",
    "USA_list: \n",
    "['[Freeman, Scott; Eddy, Sarah L.; McDonough, Miles; Okoroafor, Nnadozie; Jordt, Hannah; Wenderoth, Mary Pat] Univ Washington, Dept Biol, Seattle, WA 98195 USA; [Smith, Michelle K.] Univ Maine, Sch Biol & Ecol, Orono, ME 04469 USA',\n",
    " '[Henderson, Charles] Western Michigan Univ, Dept Phys, Kalamazoo, MI 49008 USA; [Henderson, Charles] Western Michigan Univ, Mallinson Inst Sci Educ, Kalamazoo, MI 49008 USA; [Finkelstein, Noah] Univ Colorado, Dept Phys, Boulder, CO 80309 USA',...\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# C-1 info: 所有作者列表\n",
    "info = df['C1'].fillna(\"空缺值\").tolist()\n",
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# C-2 请使用info列表\n",
    "China_list = []\n",
    "for item in info:\n",
    "    if \"China\" in item:\n",
    "        China_list.append(item)\n",
    "China_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  Q4 （20分）🌶🌶 中等\n",
    "\n",
    "-----------------\n",
    "* 尝试用python代码取出 邮政编码（注：5位的数字信息）并存进邮编列表中。\n",
    "* 答案示例：\n",
    "```\n",
    "['98195',\n",
    " '04469',\n",
    " '49008',\n",
    " '49008',\n",
    " '80309',\n",
    " ...\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# D-1 新建邮编_list空列表，后续得到邮编信息请增加进此列表\n",
    "邮编_list = []"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# D-2 请使用info列表数据筛选并尝试获取所有的邮编信息\n",
    "for i in info:\n",
    "    for j in i.split(' '):\n",
    "        if j.isdigit() and len(j)==5:\n",
    "            邮编_list.append(j)            \n",
    "邮编_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Q5 20分 🌶🌶🌶 难\n",
    "\n",
    "----------------\n",
    "* 尝试将年份列表中的值转换成整数,并统计每一年出现的次数和空缺值出现的次数，并以字典的形式展现。\n",
    "* 答案示例：\n",
    "```\n",
    "{2014: 58,\n",
    " 2011: 17,\n",
    "...\n",
    " '空缺值': 60}\n",
    " ```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# E-1 数据准备（所有年份数据列表） 学生直接执行\n",
    "PY_list = df_C1['PY'].fillna(\"空缺值\").to_list()\n",
    "PY_list"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "# 方案一\n",
    "PY_list_no_空缺值 = []\n",
    "for i,j in enumerate(PY_list):\n",
    "    if j !=\"空缺值\":\n",
    "        PY_list_no_空缺值.append(int(j))\n",
    "# PY_list_no_空缺值\n",
    "PY_count = {}\n",
    "for item in PY_list_no_空缺值:\n",
    "    if item in PY_count:\n",
    "        PY_count[item] += 1\n",
    "    else:\n",
    "        PY_count[item] = 1\n",
    "PY_count[\"空缺值\"] = PY_list.count(\"空缺值\")\n",
    "PY_count\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# E-2 尝试将年份列表中的值转换成整数,并统计每一年出现的次数和空缺值出现的次数，最终以字典的形式展现，示例代码参考上述markdown\n",
    "PY_count = {\"空缺值\":PY_list.count(\"空缺值\")}\n",
    "for item in PY_list:\n",
    "    if type(item) == float:\n",
    "        PY_count[int(item)] = PY_list.count(item)\n",
    "PY_count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Q6 20分 🌶🌶🌶 难 \n",
    "\n",
    "--------------\n",
    "* 按照列表下表索引建立字典，在字典中拆分作者，并统计每篇文章作者的人数\n",
    "> 1. 请以每篇文章的index作为字典的key；\n",
    "> 2. value中仍为字典，字典的key分别有：\n",
    ">> 1. \"author_list\"：存储作者列表\n",
    ">> 2. \"author_num\":统计作者个数\n",
    ">> 3. 结果示例如下:\n",
    "```\n",
    "{0: {'author_list': ['Han, XY',\n",
    "   ' Stocking, G',\n",
    "   ' Gebbie, MA',\n",
    "   ' Appelbaum, RP'],\n",
    "  'author_num': 4},\n",
    " 1: {'author_list': ['De Castell, S',\n",
    "   ' Larios, H',\n",
    "   ' Jenson, J',\n",
    "   ' Smith, DH'],\n",
    "  'author_num': 4},\n",
    " 2: {'author_list': ['Putansu, SR'], 'author_num': 1},\n",
    " 3: {'author_list': ['Cira, NJ',\n",
    "   ' Chung, AM',\n",
    "   ' Denisin, AK',\n",
    "   ' Rensi, S',\n",
    "   ' Sanchez, GN',\n",
    "   ' Quake, SR',\n",
    "   ' Riedel-Kruse, IH'],\n",
    "  'author_num': 7},\n",
    "...后面省略\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# F-1 （勿改动，执行即可） 请先观察AU_list后再做题\n",
    "AU_list = df.AU.to_list()\n",
    "AU_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# F-2 \n",
    "AU_dict = {}\n",
    "for i,item in enumerate(AU_list):\n",
    "#     print(item.split(';'))\n",
    "    AU_dict[i] = str(item).split(';')\n",
    "AU_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "author_dict = {}\n",
    "for k,v in AU_dict.items():\n",
    "    author_dict[k] = {\"author_list\":AU_dict[k],\"author_num\":len(v)}       \n",
    "author_dict"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#终00X （勿改动，执行即可）存入检查数据\n",
    "import PandasCourse as PC\n",
    "PC.dump_answers(locals(),_which_)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#  终 （勿改动，执行即可）回报答题分数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#终001 （勿改动，执行即可）回报答题分数\n",
    "import PandasCourse as PC\n",
    "\n",
    "score_details = PC.score_answers(locals(), _which_)\n",
    "score_details"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "339.111px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
